package org.example.service.util.md.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.MdEditFileMapper;
import org.example.model.common.AppHttpCodeEnum;
import org.example.model.common.ResponseResult;
import org.example.model.user.dtos.UserIdentityDto;
import org.example.model.util.dtos.md.MdEditFileDto;
import org.example.model.util.entities.md.MdEditFile;
import org.example.model.util.vos.md.ModifyMdEditFileVo;
import org.example.service.util.md.MdEditFileService;
import org.example.service.util.md.MdFileContentService;
import org.example.utils.UserIdThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * md编辑器文件信息服务层实现
 */
@Service
@Transactional
@Slf4j
public class MdEditFileServiceImpl extends ServiceImpl<MdEditFileMapper, MdEditFile> implements MdEditFileService {

    @Autowired
    private MdEditFileMapper mdEditFileMapper;

    @Autowired
    private MdFileContentService mdFileContentService;

    /**
     * 添加一条md编辑器文件信息
     */
    @Override
    public ResponseResult<Boolean> InsertMdEditFile(ModifyMdEditFileVo modifyMdEditFileVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        // 参数审查
        if (modifyMdEditFileVo == null) {
            // 缺少参数
            result.error(AppHttpCodeEnum.PARAM_REQUIRE.getCode(), AppHttpCodeEnum.PARAM_REQUIRE.getErrorMessage());
            return result;
        }
        MdEditFile mdEditFile = new MdEditFile();
        BeanUtils.copyProperties(modifyMdEditFileVo, mdEditFile);
        this.HandleModifyMdEditFile(mdEditFile);
        Boolean flag = mdEditFileMapper.insert(mdEditFile) > 0;
        if (!flag) {
            result.error(AppHttpCodeEnum.SERVER_ERROR.getCode(), AppHttpCodeEnum.SERVER_ERROR.getErrorMessage());
            return result;
        }
        flag = mdFileContentService.InsertMdFileContent(mdEditFile.getId());
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), flag, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 添加或修改md文件信息统一操作
     */
    private void HandleModifyMdEditFile (MdEditFile mdEditFile) {
        // 获取进程中的用户数据
        UserIdentityDto userIdentityDto = UserIdThreadLocalUtils.getUser();
        if (mdEditFile.getId() == 0) {
            mdEditFile.setCreateUserId(userIdentityDto.getId());
            mdEditFile.setCreateTime(LocalDateTime.now());
            mdEditFile.setIsDeleted(false);
        }
        mdEditFile.setUpdateUserId(userIdentityDto.getId());
        mdEditFile.setUpdateTime(LocalDateTime.now());
    }

    /**
     * 根据id修改一条md编辑器文件信息
     */
    @Override
    public ResponseResult<Boolean> UpdateMdEditFileById(ModifyMdEditFileVo modifyMdEditFileVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        // 参数审查
        if (modifyMdEditFileVo == null || modifyMdEditFileVo.getId() <= 0) {
            // 缺少参数 工具id
            result.error(AppHttpCodeEnum.PARAM_REQUIRE.getCode(), AppHttpCodeEnum.PARAM_REQUIRE.getErrorMessage());
            return result;
        }
        MdEditFile mdEditFile = new MdEditFile();
        BeanUtils.copyProperties(modifyMdEditFileVo, mdEditFile);
        this.HandleModifyMdEditFile(mdEditFile);
        Boolean flag = mdEditFileMapper.updateById(mdEditFile) > 0;
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), flag, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 根据id删除一条md编辑器文件信息
     */
    @Override
    public ResponseResult<Boolean> DeleteMdEditFileById(Integer id) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        // 参数审查
        if (id <= 0) {
            // 缺少参数 工具信息id
            result.error(AppHttpCodeEnum.PARAM_REQUIRE.getCode(), AppHttpCodeEnum.PARAM_REQUIRE.getErrorMessage());
            return result;
        }
        MdEditFile mdEditFile = mdEditFileMapper.selectById(id);
        mdEditFile.setIsDeleted(true);
        this.HandleModifyMdEditFile(mdEditFile);
        Boolean flag = mdEditFileMapper.updateById(mdEditFile) > 0;
        if (!flag) {
            result.error(AppHttpCodeEnum.SERVER_ERROR.getCode(), AppHttpCodeEnum.SERVER_ERROR.getErrorMessage());
            return result;
        }
        flag = mdFileContentService.DeleteMdFileContentById(id);
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), flag, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 获取md文件树
     */
    @Override
    public ResponseResult<List<MdEditFileDto>> GetMdEditFileTree() {
        // 获取所有的父节点
        List<MdEditFileDto> mdEditFileDtoList = this.HandleSonNodeList(0);
        for (MdEditFileDto mdEditFileDto : mdEditFileDtoList) {
            this.GetMdEditFileDtoDetailById(mdEditFileDto);
        }
        ResponseResult<List<MdEditFileDto>> result = new ResponseResult<>();
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), mdEditFileDtoList, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    // 根据id获取文件信息详情
    private void GetMdEditFileDtoDetailById (MdEditFileDto mdEditFileDto) {
        List<MdEditFileDto> mdEditFileDtoList = this.HandleSonNodeList(mdEditFileDto.getId());
        if (mdEditFileDtoList.size() == 0) return;
        for (MdEditFileDto editFileDto : mdEditFileDtoList) {
            this.GetMdEditFileDtoDetailById(editFileDto);
        }
        mdEditFileDto.setMdEditFileDtoList(mdEditFileDtoList);
    }

    // 根据父id获取子列表
    private List<MdEditFileDto> HandleSonNodeList (int parentId) {
        LambdaQueryWrapper<MdEditFile> mdEditFileQuery = new LambdaQueryWrapper<>();
        mdEditFileQuery.eq(MdEditFile::getParentId, parentId)
                .eq(MdEditFile::getIsDeleted, false);
        // 获取所有的父节点
        List<MdEditFile> mdEditFileList = mdEditFileMapper.selectList(mdEditFileQuery);
        List<MdEditFileDto> mdEditFileDtoList = new ArrayList<>();
        MdEditFileDto mdEditFileDto = null;
        for (MdEditFile mdEditFile : mdEditFileList) {
            mdEditFileDto = new MdEditFileDto();
            BeanUtils.copyProperties(mdEditFile, mdEditFileDto);
            mdEditFileDtoList.add(mdEditFileDto);
        }
        return mdEditFileDtoList;
    }
}
